package edu.cicese.alz.reports;

import javax.swing.DefaultListModel;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;

/**
 * Created by: Eduardo Quintana Contreras
 * Date: 3/08/12
 * Time: 01:14 PM
 */
public class BlockCombinedPreview extends BlockPreview {

	private int accNoDataOpacity = 0;
	private int accGeneralOpacity = 255;

	private int gpsNoDataOpacity = 0;
	private int gpsHomeOpacity = 255;
	private int gpsOutOpacity = 255;

	private int nfcNoDataOpacity = 0;
	private int nfcGeneralOpacity = 255;

	private DefaultListModel layers;

	public BlockCombinedPreview() {
		loadAccelerometerValues();
		gpsValues = new int[height / 30][width / 5];
		setGPSValues();
	}

	public void setLayers(DefaultListModel layers) {
		this.layers = layers;
	}

	public void setAccNoDataOpacity(int accNoDataOpacity) {
		this.accNoDataOpacity = accNoDataOpacity;
	}

	public void setAccGeneralOpacity(int accGeneralOpacity) {
		this.accGeneralOpacity = accGeneralOpacity;
	}

	public void setGpsNoDataOpacity(int gpsNoDataOpacity) {
		this.gpsNoDataOpacity = gpsNoDataOpacity;
	}

	public void setGpsHomeOpacity(int gpsHomeOpacity) {
		this.gpsHomeOpacity = gpsHomeOpacity;
	}

	public void setGpsOutOpacity(int gpsOutOpacity) {
		this.gpsOutOpacity = gpsOutOpacity;
	}

	public void setNfcNoDataOpacity(int nfcNoDataOpacity) {
		this.nfcNoDataOpacity = nfcNoDataOpacity;
	}

	public void setNfcGeneralOpacity(int nfcGeneralOpacity) {
		this.nfcGeneralOpacity = nfcGeneralOpacity;
	}

	@Override
	public void paintComponent(Graphics g) {
		Graphics2D g2 = (Graphics2D) g;
		g2.clearRect(0, 0, getWidth(), getHeight());

		for (int i = layers.size() - 1; i >= 0; i--) {
			switch ((String) layers.get(i)) {
				case "Accelerometer":
					paintAccelerometer(g2);
					break;
				case "GPS":
					paintGPS(g2);
					break;
				case "NFC":
					paintNFC(g2);
					break;
			}
		}
	}

	private void paintAccelerometer(Graphics2D g2) {
		for (int y = 0; y < height / 30; y++) {
			for (int x = 0; x < width; x++) {
				int value = accValues[y][x];
				Color color = accColors[value];

				if (value == 0) {
					g2.setColor(new Color(color.getRed(), color.getGreen(), color.getBlue(), accNoDataOpacity));
				}
				else {
					g2.setColor(new Color(color.getRed(), color.getGreen(), color.getBlue(), accGeneralOpacity));
				}

				g2.drawLine(x, y * 30, x, y * 30 + 30);
			}
		}
	}

	private void paintGPS(Graphics2D g2) {
		for (int y = 0; y < height / 30; y++) {
			for (int x = 0; x < width / 5; x++) {
				int value = gpsValues[y][x];
				Color color = gpsColors[value];

				int opacity;
				switch (value) {
					case 0:
						opacity = gpsNoDataOpacity;
						break;
					case 1:
						opacity = gpsHomeOpacity;
						break;
					default:
						opacity = gpsOutOpacity;
				}
				g2.setColor(new Color(color.getRed(), color.getGreen(), color.getBlue(), opacity));
				g2.fillRect(x * 5, y * 30, 5, 30);
			}
		}
	}

	private void paintNFC(Graphics2D g2) {
		g2.setColor(new Color(colorNfcNoData.getRed(), colorNfcNoData.getGreen(), colorNfcNoData.getBlue(), nfcNoDataOpacity));
		g2.fillRect(0, 0, this.getWidth(), this.getHeight());

		for (BlockColor blockColor : nfcValues) {
			if (blockColor.getNfcTag().getProfileCheckBox().isSelected()) {
				int x = blockColor.getX();
				int y = blockColor.getY();

				Color color = blockColor.getNfcTag().getColor();
				g2.setColor(new Color(color.getRed(), color.getGreen(), color.getBlue(), nfcGeneralOpacity));

				g2.drawLine(x, y * 30, x, y * 30 + 30);
			}
		}
	}
}
